<?php
declare(strict_types=1);
/**
 * 商城商品变化通知
 */
namespace Service\CommodityService;
use Service\BaseService;
use App\Amqp\Producer\ShopCommodityMessageProducer;
use Hyperf\Amqp\Producer;
use App\Model\PssModel\GoodsCart;
use App\Model\PssModel\ShopGoodsGroupList;
use App\Model\PssModel\ShopSeckillGoodsList;
use Service\Code\Normal;

class ShopCommodityChange extends BaseService
{
    /**
     * 商品正常变更推送
     * 消息发布到MQ
     * @param array $body
     */
    public function goodsChange(array $body)
    {
        $message = [
            "MessageType"   => GoodsNotifyType::SHOP_COMMODITY_CHANGE,
            "DataList"      => $body
        ];
        return $this->publishMsg($message);
    }

    /**
     * 商品所有下架
     * 消息发布到MQ
     * @param array $body
     */
    public function goodsAllDown(array $body = [])
    {
        $message = [
            "MessageType"   => GoodsNotifyType::SHOP_COMMODITY_ALL_DEL,
            "DataList"      => $body
        ];
        return $this->publishMsg($message);
    }

    /**
     * 商品部分下架
     * 消息发布到MQ
     * @param array $body
     */
    public function goodsDel(array $body)
    {
        $message = [
            "MessageType"   => GoodsNotifyType::SHOP_COMMODITY_DEL,
            "DataList"      => $body
        ];
        return $this->publishMsg($message);
    }
    
    /**
     * 获取需要向es里面更新的数据字段
     * @param array $goodsList
     */
    public function getSearchGoodsListByColumns( array $goodsList = [] )
    {
        $AuthData = [];
        foreach ($goodsList as $key => $goods )
        {
            $SpecList   = @json_decode($goods['SpecList'],true);
            $firstSpec  = current($SpecList);
            $SpecPrice  = $firstSpec['SpecFreightPrice']??$firstSpec['SpecPrice'];
            //写入页面显示的价格(),前台要参与价格排序
            if( $goods['IsHaveTaxRate'] != 1 ) {
                $rate = bcadd((string)$goods['TaxRate'],'1.0000',4);
                $SpecPrice = bcmul((string)$SpecPrice,$rate,2);
            }
    
            $AuthData[] = [
                'GoodsName'                 => $goods['Name'],
                'CommodityType'             => $goods['CommodityType'],
                'BarCode'                   => $goods['BarCode'],
                'SpuName'                   => $goods['SpuName'],
                'SkuID'                     => $goods['SkuID'],
                'ID'                        => $goods['SkuID'],
                'SKUCode'                   => $goods['SKUCode'],
                'HasTaxCostPrice'           => $SpecPrice,//商城前台列表展示价格
                'FirstClassificationID'     => $goods['FirstClassificationID'],
                'SecondClassificationID'    => $goods['SecondClassificationID'],
                'ThirdClassificationID'     => $goods['ThirdClassificationID'],
                'BrandID'                   => $goods['BrandID'],
            ];
            unset($goods);
        }
        return $AuthData;
    }
    
    /**
     * 删除商城商品里面购物车之后金额上架
     * @param array $skuIds
     */
    public function recoverShopActivityGoodsUpBySkuIDS( array $skuIds )
    {
        //上架
        GoodsCart::instance()->whereIn('goods_id',$skuIds)->update([
            'is_down'   => 0,
            'md'        => time(),
        ]);
    }
    
    /**
     * 删除商城商品里面秒杀商品、分组商品、购物车
     * @param array $skuIds
     */
    public function removeShopActivityGoodsDownBySkuIDS( array $skuIds )
    {
        //商城购物车下架
        GoodsCart::instance()->whereIn('goods_id',$skuIds)->update([
            'is_down'   => 1,
            'md'        => time(),
        ]);
        //商品分组
        ShopGoodsGroupList::instance()->whereIn('goods_id',$skuIds)->delete();
        //商品秒杀
        ShopSeckillGoodsList::instance()->whereIn('goods_id',$skuIds)->delete();
    }
    
    /**
     * 发布消息队列
     * @param array $msg
     */
    public function publishMsg( array $msg  = [] )
    {
        try {
            $handlerMessage = new ShopCommodityMessageProducer($msg);
            $producer = container()->get(Producer::class);
            $result = $producer->produce($handlerMessage,true);
            if( $result ) {
                return Normal::returnCode(Normal::SUC,"发布成功");
            }
            return Normal::returnCode(Normal::FAIL,"操作失败，请稍微再试[100000]");
        } catch ( \Throwable $e ) {
            return Normal::returnCode(Normal::FAIL,"操作失败，请稍微再试[{$e->getMessage()},{$e->getLine()},{$e->getFile()}]");
        }
    }
}
